﻿using Areaexportimg.Model;
using Areaexportimg.Utils;
using Dapper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Areaexportimg
{
    class Business: BaseDAL<GeoDistrictEntity>
    {
        public Business()
        {
            ConnectionFactory.Config(DatabaseType.Mysql, ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        }

        public async Task<GeoDistrictEntity> Get(string id)
        {
            var wheres = new List<Wherevalue>() { new Wherevalue() { PropertyKey = "id", Value = id } };
            using (IDbConnection dbConnection = Connection)
            {
                var wherepars = SqlHelper.GetWhere(wheres, "g");
                var sql = new StringBuilder();

                sql.Append($"select g.*,d.* from {TableName} g");
                sql.Append($" left join `geodata` d on d.geodistrictid = g.id");
                sql.Append(wherepars.Item1);
                sql.Append(" order by d.pointindex");
                var entites = new List<GeoDistrictEntity>();
                await dbConnection.QueryAsync<GeoDistrictEntity, GeoDataEntity, GeoDistrictEntity>(sql.ToString(), (g, data) =>
                {
                    if (entites.Exists(m => m.Id == g.Id))
                    {
                        var entity = entites.Find(m => m.Id == g.Id);
                        entity.GeoData.Add(data);
                    }
                    else
                    {
                        g.GeoData.Add(data);
                        entites.Add(g);
                    }
                    return g;
                }, wherepars.Item2);
                return entites.FirstOrDefault();
            }
        }
        public async Task<List<GeoDistrictEntity>> GetInfos(string parentId)
        {
            return await GetParentIdItems(parentId);
        }

        private async Task<List<GeoDistrictEntity>> GetParentIdItems(string parentid, string cusSql = "")
        {
            var wheres = new List<Wherevalue>() { new Wherevalue() { PropertyKey = "parentid", Value = parentid } };
            if (parentid.IsNullOrEmpty())
            {
                wheres.Clear();
                wheres.Add(new Wherevalue()
                {
                    PropertyKey = "adminType",
                    Value = "1"
                });
            }
            using (var dbConnection = Connection)
            {
                var wherepars = SqlHelper.GetWhere(wheres, "g");
                var sql = new StringBuilder();

                if (cusSql.IsNotNullOrEmpty())
                {
                    sql.Append(cusSql);
                }
                else
                {
                    sql.Append($"select g.* from {TableName} g");
                }
                // sql.Append($" left join `geodata` d on d.geodistrictid = g.id");
                sql.Append(wherepars.Item1);
                sql.Append(" order by g.English");
                return (await dbConnection.QueryAsync<GeoDistrictEntity>(sql.ToString(), wherepars.Item2)).ToList().Select(m=>
                {
                    m.EnglishAbbrevation = Util.GetPinyin(m.NameAbbrevation).ToLower();
                    return m;
                }).ToList();
            }
        }

    }
}
